package br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.components;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.NumberFormat;
import java.text.ParseException;

import org.w3c.dom.Element;

import br.inf.ufrgs.br.usixml4cdc.rendering.UsiXMLLayoutManagerRenderer;
import br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.RendererFactory;
import br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.UsiXMLJavaDesktopInterfaceRenderer;
import br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.utils.InformationParser;
import br.inf.ufrgs.usixml4cdc.exceptions.ParsingErrorException;
import br.inf.ufrgs.usixml4cdc.exceptions.RenderingErrorException;

public class UsiXMLJavaDesktopGridBagBoxRenderer extends UsiXMLLayoutManagerRenderer {

	public UsiXMLJavaDesktopGridBagBoxRenderer(UsiXMLJavaDesktopInterfaceRenderer usiXMLInterfaceRenderer) {
		super(usiXMLInterfaceRenderer);
	}

	public UsiXMLLayoutManagerRenderer getNextRenderer() {
		return new UsiXMLJavaDesktopFlowBoxRenderer((UsiXMLJavaDesktopInterfaceRenderer) this.usiXMLInterfaceRenderer);
	}

	public String getValidElementName() {
		return "gridBagBox";
	}

	public Container processRenderization(Element element, Container contentPane) 
		throws ParsingErrorException,RenderingErrorException {
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		
		contentPane.setLayout(gridbag);
		
		int gridx = 0;
		int gridy = 0;
		int gridwidth = 0;
        int gridheight = 0; 
        double weightx = 0; 
        double weighty = 0;
        int fill = 0;
        Insets insets = null;
		
        int numChildren = element.getChildNodes().getLength();
        for (int i = 0; i < numChildren; i++) {
            if (element.getChildNodes().item(i) instanceof  Element){
                Element child = (Element) element.getChildNodes().item(i);
                try {
                        gridx = NumberFormat.getIntegerInstance().parse(child.getAttribute("gridx")).intValue();
                        gridy = NumberFormat.getIntegerInstance().parse(child.getAttribute("gridy")).intValue();
                        gridwidth = NumberFormat.getIntegerInstance().parse(child.getAttribute("gridwidth")).intValue();
                        gridheight = NumberFormat.getIntegerInstance().parse(child.getAttribute("gridheight")).intValue();
                        weightx = NumberFormat.getNumberInstance().parse(child.getAttribute("weightx")).doubleValue();
                        weighty = NumberFormat.getNumberInstance().parse(child.getAttribute("weighty")).doubleValue();

                        InformationParser parser = new InformationParser();
                        insets = parser.parseInsets(child.getAttribute("insets"));

                        String fillString = child.getAttribute("fill");

                        if (fillString.equals("both")){
                                fill = GridBagConstraints.BOTH;
                        } else if (fillString.equals("horizontal")){
                                fill = GridBagConstraints.HORIZONTAL;
                        } else if (fillString.equals("vertical")){
                                fill = GridBagConstraints.VERTICAL;
                        } else{
                                throw new RenderingErrorException("Unknown fill type");
                        }

                        constraints.gridx = gridx;
                        constraints.gridy = gridy;
                        constraints.weightx = weightx;
                        constraints.weighty = weighty;
                        constraints.gridwidth = gridwidth;
                        constraints.gridheight = gridheight;
                        constraints.fill = fill;
                        constraints.insets = insets;

                        Element constraintChild = (Element) child.getChildNodes().item(0);
                        Component component = (Component) 
                                RendererFactory.getInstance().
                                        getFirstElementRenderer((UsiXMLJavaDesktopInterfaceRenderer)this.usiXMLInterfaceRenderer).renderElement(constraintChild);

                        contentPane.add(component,constraints);


                } catch (ParseException e) {
                        e.printStackTrace();
                }

                }
        }
		
		return contentPane;
		
	}
	
	public boolean validateElement(Element element) {
		return true;
	}
}
